package Vista;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.SwingUtilities;

import Controlador.Sistema;
import DAO.DAOCliente;
import DAO.DAOModelo;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class frmModelos extends javax.swing.JFrame {
	private JButton btnAlta;
	private JTable tblModelos;
	private JScrollPane jScrollPane1;
	private JButton btnEliminar;
	private JButton btnModificar;
	private Sistema elSistema = Sistema.Getinstancia();

	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				frmModelos inst = new frmModelos();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	public frmModelos() {
		super();
		initGUI();
		llenarGrilla();
	}
	
	private void llenarGrilla(){
		// Lleno la grilla de clientes

		ArrayList<DAOModelo> modelos = elSistema.ConsultarModelos();
		
		DefaultTableModel tblModelosModel = new DefaultTableModel();
		
		tblModelosModel.addColumn("Marca");
		tblModelosModel.addColumn("Modelo");
		tblModelosModel.addColumn("Motor");
		tblModelosModel.addColumn("ModeloTipo");
		
		for (DAOModelo dam : modelos) {
			
			Vector<String> valores = new Vector<String>();
			
			valores.add(dam.getMarca());
			valores.add(dam.getModelo());
			valores.add(dam.getMotor());
			valores.add(dam.getModeloTipo().toString());
			
			tblModelosModel.addRow(valores);
		}

		tblModelos.setModel(tblModelosModel);
		
	}

	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			{
				btnAlta = new JButton();
				getContentPane().add(btnAlta);
				btnAlta.setText("Alta");
				btnAlta.setBounds(12, 12, 123, 23);
				btnAlta.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						btnAltaActionPerformed(evt);
					}
				});
			}
			{
				btnModificar = new JButton();
				getContentPane().add(btnModificar);
				btnModificar.setText("Modificar");
				btnModificar.setBounds(146, 12, 116, 23);
				btnModificar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						btnModificarActionPerformed(evt);
					}
				});
			}
			{
				btnEliminar = new JButton();
				getContentPane().add(btnEliminar);
				btnEliminar.setText("Eliminar");
				btnEliminar.setBounds(273, 12, 107, 23);
				btnEliminar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						btnEliminarActionPerformed(evt);
					}
				});
			}
			{
				jScrollPane1 = new JScrollPane();
				getContentPane().add(jScrollPane1);
				jScrollPane1.setBounds(12, 40, 737, 229);
				{
					TableModel tblModelosModel = 
						new DefaultTableModel();
					tblModelosModel = 
						new DefaultTableModel(
								new String[][] { { "One", "Two" }, { "Three", "Four" } },
								new String[] { "Column 1", "Column 2" });
					tblModelos = new JTable();
					jScrollPane1.setViewportView(tblModelos);
					tblModelos.setModel(tblModelosModel);
					tblModelos.setBounds(0, 38, 262, 171);
					tblModelos.setPreferredSize(new java.awt.Dimension(734, 202));
					tblModelos.setModel(tblModelosModel);
				}
			}
			pack();
			this.setSize(777, 314);
			
			
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
	}
	
	private void btnAltaActionPerformed(ActionEvent evt) {
		frmModelo frm = new frmModelo("A", "", "");
		frm.setLocationRelativeTo(null);
		frm.setVisible(true);
	}
	
	private void btnModificarActionPerformed(ActionEvent evt) {
		String marca = "";
		String modelo = "";
		
		TableModel tablaM;
		tablaM = (TableModel) this.tblModelos.getModel();
		
		int registro = this.tblModelos.getSelectedRow();
		
		if (registro >= 0) {
			marca = (String) tablaM.getValueAt(registro, 0);
			modelo = (String) tablaM.getValueAt(registro, 1);
		}
		else {
			return;			
		}
		
		frmModelo frm = new frmModelo("M", marca, modelo);
		frm.setLocationRelativeTo(null);
		frm.setVisible(true);	}
	
	private void btnEliminarActionPerformed(ActionEvent evt) {
		String marca = "";
		String modelo = "";
		
		TableModel tablaM;
		tablaM = (TableModel) this.tblModelos.getModel();
		
		int registro = this.tblModelos.getSelectedRow();
		
		if (registro >= 0) {
			marca = (String) tablaM.getValueAt(registro, 0);
			modelo = (String) tablaM.getValueAt(registro, 1);
		}
		else {
			return;
		}
		
		frmModelo frm = new frmModelo("E", marca, modelo);
		frm.setLocationRelativeTo(null);
		frm.setVisible(true);	}

}
